package com.example.sale.Controller;

import com.example.sale.Service.imply.MessageService;
import com.example.sale.True.Feedback;
import com.example.sale.True.Verification;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.example.sale.SelfUtil.MakeCaptcha;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/message")
public class MessageController {
   @Autowired
   private MessageService messageService;
   @Autowired
   private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @PostMapping("/Phone")
    public Map<String,Object> sendMail(@RequestParam String PhoneNumber,
                                       @RequestParam String vscode){
        String code = MakeCaptcha.generateCaptcha();
        Map<String, Object> map1 = messageService.sendMessage(PhoneNumber, code);
        //设置手机验证码的rabbitmq
        Verification verification = new Verification();
        verification.setKey(vscode);
        verification.setVscode(code);
//        rabbitTemplate.convertAndSend("hmall.direct","blue",verification);
        //测试是是否把验证码给存入进去了
        stringRedisTemplate.opsForValue().set(vscode, code, 300, TimeUnit.SECONDS);
        //设置这个手机验证码的
        System.out.println(map1);
        return map1;
    }
    @PostMapping("/CheckExist")
    public Map<String,Object> CheckExist(@RequestParam String vscode,
                                       @RequestParam String key){
        String Code = stringRedisTemplate.opsForValue().get(key);
        Map<String,Object>map =new HashMap<>();
        if(Code.equals(vscode)){
            map.put("status","true");
            return map;
        }
        else{
            map.put("status","false");
        }
        return map;
    }
}
